﻿Imports FireAndIceSoft.dbFireAndIceDataSetTableAdapters
Public Class ClonarRol

    Public Sub CargarLosGrid()
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.usuarios' table. You can move, or remove it, as needed.
        Me.UsuariosTableAdapter.Fill(Me.DbFireAndIceDataSet.usuarios)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.seg_roldetalle' table. You can move, or remove it, as needed.
        Me.Seg_roldetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.seg_roldetalle)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet._roles' table. You can move, or remove it, as needed.
        Me.RolesTableAdapter1.Fill(Me.DbFireAndIceDataSet._roles)
    End Sub

    Public Sub limpiarTextos()
        nombre.Text = ""
        descripcion.Text = ""

    End Sub
    Private Sub RolesBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles RolesBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.RolesBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub ClonarRol_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CargarLosGrid()
        limpiarTextos()
        rolSeleccionado.Text = "Seleccione un Rol"

    End Sub

    Private Sub SimpleButton1_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton1.Click
        Try
            If nombre.Text.Length = 0 Then
                ErrorProvider1.SetError(nombre, "Debe ingresar un nombre para el Rol a clonar")
                Return
            End If
            ErrorProvider1.SetError(nombre, Nothing)

            If descripcion.Text.Length = 0 Then
                ErrorProvider1.SetError(descripcion, "Debe ingresar una descripción para el Rol a clonar")
                Return
            End If
            ErrorProvider1.SetError(descripcion, Nothing)
            rolSeleccionado.Text = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "nombrerol")
            Dim idRol As Integer = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "idrol")
            Dim nombreRol As String = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "nombrerol")
            Using adaptadorRoles As New rolesTableAdapter1
                Dim IdRols As Integer = ConexionSGS.EjecutarQueryScalar("select max(idrol)+1 from roles")

                adaptadorRoles.Insert(IdRols, nombre.Text, descripcion.Text)

                Using adaptadorRolesDetalle As New seg_roldetalleTableAdapter
                    For Each item In adaptadorRolesDetalle.GetData
                        If item.idrol = idRol Then
                            adaptadorRolesDetalle.Insert(IdRols, item.idclase)
                        End If
                    Next
                End Using
            End Using
            CargarLosGrid()
            limpiarTextos()
            rolSeleccionado.Text = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "nombrerol")
            MsgBox("El Rol ha sido clonado satisfactoriamente", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Clonación de Rol")
        Catch ex As Exception
            MsgBox("El Rol no se pudo clonar", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Clonación de Rol")

        End Try
        


    End Sub




    Private Sub RolesGridControl_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles RolesGridControl.MouseMove
        rolSeleccionado.Text = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "nombrerol")
    End Sub
End Class